
uniform bool initBuffer;

varying vec3 normal;

uniform sampler2D tex;

uniform vec4 ambient;
uniform vec4 specular;
uniform float shininess;

void main (void) {    
    vec4 color = texture2D (tex, gl_TexCoord[0].st);        
    
    if (color.a < 1.0) discard;
    
    if (!initBuffer) {
	    vec3 n = normalize(normal);
	    //normal = 2.0 * normal - 1.0;    
	    //if (normal == vec3(0.0, 0.0, 0.0) || normal == vec3(1.0, 1.0, 1.0)) {
	    //    normal = vec3(0.0, 0.0, 1.0);
	    //}
	    //normal = normalize(normal);    
	    
	    vec3 lightDir = normalize(vec3(gl_LightSource[0].position));
	    vec4 diffuse = color;
	     
	    color = ambient * (gl_LightSource[0].ambient + gl_LightModel.ambient);
	
	    float NdotL = max(dot(n, lightDir), 0.0);
	
	    if (NdotL > 0.0) {
	        color += diffuse * gl_LightSource[0].diffuse * NdotL;
	        float NdotHV = max(dot(n, gl_LightSource[0].halfVector.xyz), 0.0);
	        color += specular * gl_LightSource[0].specular * pow(NdotHV, shininess);
	    }
    }
    gl_FragData[0] = color;
}
